/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-10-18     Meco Man     The first version
 */
#if 0
#include <lvgl.h>
#include <rtdevice.h>
#include <drv_gpio.h>

#define BUTTON0_PIN        GET_PIN(F, 4)  // 上键
#define BUTTON1_PIN        GET_PIN(F, 5)  // 下键
#define BUTTON2_PIN        GET_PIN(F, 6)  // 左键
#define BUTTON3_PIN        GET_PIN(C, 13) // 右键

lv_indev_t * indev_touchpad;

static lv_coord_t touchpad_x = LCD_W/2;
static lv_coord_t touchpad_y = LCD_H/2;

/*Initialize your touchpad*/
static void touchpad_init(void)
{
    rt_pin_mode(BUTTON0_PIN, PIN_MODE_INPUT);
    rt_pin_mode(BUTTON1_PIN, PIN_MODE_INPUT);
    rt_pin_mode(BUTTON2_PIN, PIN_MODE_INPUT);
    rt_pin_mode(BUTTON3_PIN, PIN_MODE_INPUT);
}

/*Return true is the touchpad is pressed*/
static bool touchpad_is_pressed(void)
{
    return (rt_pin_read(BUTTON0_PIN) == PIN_LOW || 
            rt_pin_read(BUTTON1_PIN) == PIN_LOW ||
            rt_pin_read(BUTTON2_PIN) == PIN_LOW ||
            rt_pin_read(BUTTON3_PIN) == PIN_HIGH);
}



/*Get the x and y coordinates if the touchpad is pressed*/
static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
{
    if(rt_pin_read(BUTTON0_PIN) == PIN_LOW) {      // 上键
        touchpad_y = (touchpad_y > 10) ? touchpad_y - 10 : 0;
    }
    if(rt_pin_read(BUTTON1_PIN) == PIN_LOW) {      // 下键
        touchpad_y = (touchpad_y < LCD_H - 10) ? touchpad_y + 10 : LCD_H - 1;
    }
    if(rt_pin_read(BUTTON2_PIN) == PIN_LOW) {      // 左键
        touchpad_x = (touchpad_x > 10) ? touchpad_x - 10 : 0;
    }
    if(rt_pin_read(BUTTON3_PIN) == PIN_HIGH) {     // 右键
        touchpad_x = (touchpad_x < LCD_W - 10) ? touchpad_x + 10 : LCD_W - 1;
    }

    *x = touchpad_x;
    *y = touchpad_y;
}

/*Will be called by the library to read the touchpad*/
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static lv_coord_t last_x = 0;
    static lv_coord_t last_y = 0;

    /*Save the pressed coordinates and the state*/
    if(touchpad_is_pressed()) {
        touchpad_get_xy(&last_x, &last_y);
        data->state = LV_INDEV_STATE_PR;
    }
    else {
        data->state = LV_INDEV_STATE_REL;
    }

    /*Set the last pressed coordinates*/
    data->point.x = last_x;
    data->point.y = last_y;
}

void lv_port_indev_init(void)
{
    static lv_indev_drv_t indev_drv;
    touchpad_init();

    /*Register a touchpad input device*/
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = touchpad_read;
    indev_touchpad = lv_indev_drv_register(&indev_drv);
}


#endif